package de.luisoft.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.luisoft.dao.MyDAO;
import de.luisoft.dto.WahlDTO;
import de.luisoft.ui.model.ProjectModel;

public class ListUserServlet extends AbstractListServlet {

	private static final Logger log = Logger.getLogger(ListUserServlet.class.getName());

	/**
	 * 
	 */
	private static final long serialVersionUID = 7161146165061872986L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		log.info("list user");

		resp.setContentType("text/plain");
		String del = req.getParameter("delete");
		MyDAO my = new MyDAO();
		if (del != null && del.equals("true")) {
			my.delete();
			resp.getWriter().append("ok");
		} else {
			resp.getWriter().append(my.list());
		}
		
		final List<WahlDTO> l = my.getAllVerified(true);
		List<WahlDTO> nv = my.getAllVerified(false);
		resp.getWriter().append("\nAll entries (verif+nicht verif.): " + nv.size());
		resp.getWriter().append("\nAll verified entries    : " + l.size() + "\n\n\n");
		resp.getWriter().append("\nverifiziert:      (" + l.size() + ")\n");
		for (WahlDTO w : l) resp.getWriter().append("\n" + w.getVorname() + " " + w.getName() + " (" + w.getKlasse() + "): " + w.getSelect());

		nv.removeAll(l);
		resp.getWriter().append("\n\nnicht verifiziert (" + nv.size() + ") " + nv);

		resp.getWriter().append("\n---------------------------------------------------------------------------:\n");

		resp.getWriter().append("\nProjekt-Schülerzuordnung:\n");
		resp.getWriter().append("  (in Klammern die Anz. Schüler/max. Schüleranzahl)\n\n");
		
		Map<String, ProjectModel> projectbyName = new LinkedHashMap<String, ProjectModel>();
		Map<String, List<WahlDTO>> t = getVotesByProject(projectbyName);

		Map<String, List<String>> selectByStudent = new HashMap<String, List<String>>();
		for (WahlDTO w : l) selectByStudent.put(
				w.getName()+"," + w.getVorname()+","+w.getKlasse(), w.getSelect());

		int nbProjects = 0;
		int nbTotalCap = 0;
		int nbStudents = 0;
		List<String> projectsFull = new ArrayList<String>();
		LinkedHashSet<WahlDTO> notassigned = new LinkedHashSet<WahlDTO>(l);

		Map<Integer, Integer> cntByplace = new HashMap<Integer, Integer>();

		try {
			for (String p : t.keySet()) {
				List<WahlDTO> ws = t.get(p);

				resp.getWriter().append("\nProjekt: " + p
						+ " ("
						+ ws.size() + "/" 
						+ projectbyName.get(p).getCapacity() + ")\n");
				nbProjects++;
				nbTotalCap += projectbyName.get(p).getCapacity();
				if (ws.size() == projectbyName.get(p).getCapacity()) {
					projectsFull.add(p);
				}
				int j = 1;
				for (WahlDTO w : ws) {
					List<String> stVotes = selectByStudent.get(w.getName()+","+w.getVorname()
							+","+w.getKlasse());
					int id = 0;
					if (stVotes != null)
						id = stVotes.indexOf(p) + 1;
					resp.getWriter().append("    " + j + "." + w.getName() +", " + w.getVorname()
							+ " " + w.getKlasse() + " (" + w.getEmail() + ") " + id
					
									+ "\n");
					Integer cnt = cntByplace.get(id);
					if (cnt == null) {
						cntByplace.put(id, new Integer(1));
					} else {
						cntByplace.put(id, cnt + 1);
					}
					nbStudents++;
					j++;
					notassigned.remove(w);
				}
			}
			
			resp.getWriter().append("\nEs sind " + projectsFull.size() + "/" + nbProjects
					+ " Projekte ausgebucht: " + projectsFull);
			resp.getWriter().append("\n\nAnzahl Projektplätze   : " + nbTotalCap);
			resp.getWriter().append("\n\nAnzahl zugeord. Schüler: " + nbStudents);
			resp.getWriter().append("\n\nAnzahl der 1.Wahl, 2.Wahl, ..." + cntByplace);
			resp.getWriter().append("\n   z.B. 1=" + cntByplace.get(1) + " bedeutet, dass bei "
					+ cntByplace.get(1) + " Schülern die 1. Wahl berücksichtigt werden konnte");
			resp.getWriter().append("\n\nNicht zugeordnete Schüler:" + notassigned.size() + ":");
			int i = 1;
			for (WahlDTO wd : notassigned)
				resp.getWriter().append("\n" + (i++) + ". " + wd.getVorname() + " " + wd.getName() + " (" + wd.getKlasse() + "): " + wd.getSelect());
			resp.getWriter().append("\n");
		} catch (Exception e) {
			e.printStackTrace();
			log.log(Level.SEVERE, "failed", e);
			resp.getWriter().append(e.toString());
		}
		resp.getWriter().close();
	}
}
